package jk.spider.core.event.URLFound;

import java.util.Set;

import jk.spider.core.SpiderController;
import jk.spider.core.event.CoreEvent;
import jk.spider.core.event.impl.URLFoundEvent;
import jk.spider.core.task.work.SpiderHttpURLTaskFactory;
import jk.spider.model.Resource;
import jk.spider.util.ParserUtil;
import jk.spider.util.SpiderUtil;

import org.apache.log4j.Logger;

public abstract class URLFound {
	protected static final Logger log = Logger.getLogger(URLFound.class);
	protected String reg;
	protected Set<String> filterSet;
	
	public URLFound(String reg) {
		this.reg = reg;
	}
	
	public URLFound(String reg, Set<String> filterSet) {
		this.reg = reg;
		this.filterSet = filterSet;
	}
	
	public abstract String getUrlFoundType();

	public abstract void initialize();
	
	public abstract boolean findUrl(URLFoundEvent event);
	
	public boolean isFilter(String filter) {
		if(SpiderUtil.isStringNull(filter))
			return true;
		return this.filter(filter);
	}
	
	protected boolean isUrlValid(String url) {
		return url.matches(reg);
	}
	
	protected boolean findDetail(String dUrl, Resource eventRes, SpiderController controller) {
		return findDetail(dUrl, CoreEvent.COREEVENT_DETAIL, eventRes, controller);
	}
	
	protected boolean findDetail(String dUrl, String type, Resource eventRes, SpiderController controller) {
		Resource deRes = new Resource();
		deRes.setUrl(dUrl);
		deRes.setPaId(eventRes.getRId());
		deRes.setType(type);
		if(controller.getStorage().getResourceDAO().create(deRes)) {
			log.info("[FOUND]- [DETAIL] - [" + deRes.getUrl() + "] PAID [" + deRes.getPaId() + "]");
			controller.getContent().schedule(SpiderHttpURLTaskFactory.createSpiderHttpUrlTask(controller, deRes));
			return true;
		}
		return false;
	}
	
	protected void findList(String dUrl, Resource eventRes, SpiderController controller) {
		Resource liRes = new Resource();
		liRes.setUrl(dUrl);
		Resource dbRes = controller.getStorage().getResourceDAO().getResource(liRes.getUrl());
		if(dbRes == null) {
			liRes.setType(CoreEvent.COREEVENT_LIST);
			liRes.setPaId(eventRes.getPaId());
			if(controller.getStorage().getResourceDAO().create(liRes)) { 
				controller.getContent().schedule(SpiderHttpURLTaskFactory.createSpiderHttpUrlTask(controller, liRes));
				log.info("[FOUND] * [LIST] * [" + liRes.getUrl() + "] PAID ["+ liRes.getPaId() +"]");
			}
		}
	}
	
	protected void findReview(String dUrl, Resource eventRes, SpiderController controller) {
		Resource rvRes = new Resource();
		rvRes.setUrl(dUrl);
		Resource dbRes = controller.getStorage().getResourceDAO().getResource(rvRes.getUrl());
		if(dbRes == null) {
			rvRes.setType(CoreEvent.COREEVENT_REVIEW);
			rvRes.setPaId(eventRes.getRId());
			if(controller.getStorage().getResourceDAO().create(rvRes)) { 
				controller.getContent().schedule(SpiderHttpURLTaskFactory.createSpiderHttpUrlTask(controller, rvRes));
				log.info("[FOUND] * [REVIEW] * [" + rvRes.getUrl() + "] PAID ["+ rvRes.getPaId() +"]");
			}
		}
	}
	
	protected void findDian(String dUrl, String type, Resource eventRes, SpiderController controller) {
		if(findDetail(dUrl, type, eventRes, controller)) {
			String kbId = ParserUtil.getValue("(\\d+)", dUrl);
			String rvUrl = "http://dian.koubei.com/dian/appraisement.html?domain=" + kbId + "&appraisementCount=328";
			Resource paRes = controller.getStorage().getResourceDAO().getResource(dUrl);
			findReview(rvUrl, paRes, controller);
		}
	}
	
	private final boolean filter(String filter) {
		if(filterSet == null || filterSet.size() == 0)
			return true;
		return filterSet.contains(filter.toLowerCase());
	}
}
